package cn.wolfcode.controller;

import cn.wolfcode.domain.Strategy;
import cn.wolfcode.domain.StrategyCatalog;
import cn.wolfcode.domain.StrategyTheme;
import cn.wolfcode.query.StrategyQuery;
import cn.wolfcode.repository.StrategyRepository;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IStrategyCatalogService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.service.IStrategyThemeService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @RequestMapping("list")
    public Object list(Model model,StrategyQuery qo){
        Page query = strategyService.query(qo);
        model.addAttribute("qo",qo);
        model.addAttribute("page",query);
        return "/strategy/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){

        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }

    @RequestMapping("input")
    public String saveOrUpdate(Model model,String id){
        if (StringUtils.hasLength(id)){
            //strategy
            model.addAttribute("strategy",strategyService.get(id));
        }
        //catalogs
        model.addAttribute("catalogs",strategyCatalogService.queryList());
        //themes
        List<StrategyTheme> themes = strategyThemeService.listAll();
        model.addAttribute("themes",themes);
        return "/strategy/input";
    }


    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        if (id != null){
            strategyService.delete(id);
            return JsonResult.success();
        }
        return JsonResult.defaultError();
    }

    @RequestMapping("changeState")
    @ResponseBody
    public Object changeState(int state,String id){
        if (StringUtils.hasLength(id)){
            strategyService.changeState(id,state);
        }
        return JsonResult.success();
    }
}
